package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.bo.AdminBO;
import com.imooc.mapper.AdminMapper;
import com.imooc.pojo.Admin;
import com.imooc.service.AdminService;
import com.imooc.utils.MD5Utils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 慕聘网运营管理系统的admin账户表，仅登录，不提供注册 服务实现类
 * </p>
 *
 * @author liansen
 * @since 2022-12-15
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public boolean adminLogin(AdminBO adminBO) {
        //通过用户名获取用户信息
        Admin admin = getAdminInfo(adminBO);
        if(admin == null){
            return false;
        }
        //获取盐值
        String slat = admin.getSlat();
        //对用户传来的密码加盐之后,和数据库中的密码作对比
        String md5Str = MD5Utils.encrypt(adminBO.getPassword(), slat);
        return md5Str.equalsIgnoreCase(admin.getPassword());
    }


    @Override
    public Admin getAdminInfo(AdminBO adminBO) {
        //通过用户名获取用户信息
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<Admin>()
                .eq(Admin::getUsername, adminBO.getUsername());
        return baseMapper.selectOne(wrapper);

    }
}
